﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using NET.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace NET.Reflection
{
    [TestClass]
    public class Main
    {

        [TestMethod]
        public void ReflectionTest()
        {
            var type = Type.GetType("NET.Reflection.People");
            Console.WriteLine($"type:{type}");

            var obj = Activator.CreateInstance(type, "MyName", 100);

            var method = type.GetMethod("ToString");
            var msg1 = method.Invoke(obj, null);
            Console.WriteLine($"obj1:{msg1}");

            var normalFields = type.GetFields();
            var fields = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            Console.WriteLine($"normalFields.length:{normalFields.Length}");
            Console.WriteLine($"fields.length:{fields.Length}");

            var fname = type.GetField("Name");
            fname.SetValue(obj, "MyName666");
            var fage = type.GetField("Age", BindingFlags.NonPublic | BindingFlags.Instance);
            fage.SetValue(obj, 222);

            var msg2 = method.Invoke(obj, null);
            Console.WriteLine($"obj2:{msg2}");

            var attr1 = fname.GetCustomAttribute<MyAttAttribute>();
            var attr2 = fage.GetCustomAttribute<MyAttAttribute>();
            Console.WriteLine($"attr1:{attr1}");
            Console.WriteLine($"attr2:{attr2.Text}");
        }
    }
}
